package com.soulkun.smarthome.ui.main.home

import android.view.View
import androidx.databinding.ObservableField
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceBinding
import com.soulkun.smarthome.logic.vo.DeviceVO
import com.soulkun.smarthome.ui.main.MainNavigationDataBindingFragment
import com.soulkun.smarthome.ui.main.home.dialog.AppMainHomeDeviceCommandDialog
import com.soulkun.smarthome.ui.main.home.dialog.AppMainHomeDeviceDeleteDialog
import com.soulkun.smarthome.ui.main.home.dialog.AppMainHomeDeviceRenameDialog
import com.soulkun.smarthome.ui.main.home.popup.AppMainHomeDeviceMenuPopupWindow
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import soulkun.library.utils.screen.ScreenUtils

class AppMainHomeDeviceFragment : MainNavigationDataBindingFragment<AppFragmentMainHomeDeviceBinding>(false) {

    private val homeViewModel by navGraphViewModels<AppMainHomeViewModel>(R.id.app_navigation_main_home)
    private val deviceViewModel by viewModels<AppMainHomeDeviceViewModel> { defaultViewModelProviderFactory }

    private val renameDialog by lazy {
        AppMainHomeDeviceRenameDialog(deviceViewModel)
    }
    private val deleteDialog by lazy {
        AppMainHomeDeviceDeleteDialog(this, deviceViewModel)
    }
    private val commandDialog by lazy {
        AppMainHomeDeviceCommandDialog(deviceViewModel)
    }

    private val menuPopupWindow by lazy {
        AppMainHomeDeviceMenuPopupWindow(mContext){ popup ->
            View.OnClickListener {
                when (it.id) {
                    R.id.tv_main_home_device_menu_text_modify_device_name -> {
                        renameDialog.show(childFragmentManager, "DeviceRenameDialog")
                        popup.dismiss()
                    }
                    R.id.tv_main_home_device_menu_text_delete_device -> {
                        deleteDialog.show(childFragmentManager, "DeviceDeleteDialog")
                        popup.dismiss()
                    }
                }
            }
        }
    }

    private val tabLayoutMediator by lazy {
        TabLayoutMediator(
            requireDataBinding().tlMainHomeDeviceTabType,
            requireDataBinding().vp2MainHomeDeviceType
        ){ tab, position ->
            tab.text = deviceViewModel.typeTitleList[position]
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_device
    }

    override fun initView() {
        super.initView()
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
        requireDataBinding().vp2MainHomeDeviceType.run {
            offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle){
                override fun getItemCount(): Int {
                    return deviceViewModel.typeTitleList.size
                }

                override fun createFragment(position: Int): Fragment {
                    return if (position == 0) {
                        AppMainHomeDeviceRecentFragment(deviceViewModel)
                    }else{
                        AppMainHomeDeviceHistoryFragment(deviceViewModel)
                    }
                }
            }
            tabLayoutMediator.attach()
        }
    }

    override fun initData() {
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = deviceViewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.iv_main_home_device_icon_back -> {
                    findNavController().navigateUp()
                }
                R.id.iv_main_home_device_icon_menu ->{
                    menuPopupWindow.showAsDropDown(requireDataBinding().ivMainHomeDeviceIconMenu, 0, 0)
                }
                R.id.efab_main_home_device_btn_command -> {
                    commandDialog.show(childFragmentManager, "DeviceCommandDialog")
                }
            }
        }
    }

    override fun initListener() {
        homeViewModel.selectDeviceLiveData.observe(viewLifecycleOwner){
            if (it != null) {
                deviceViewModel.deviceLiveData.value = DeviceVO(it.id, ObservableField(it.name), it.productId, it.familyId, it.roomId)
            }
        }
    }

    override fun doOnCreate() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        tabLayoutMediator.detach()
    }

}